package com.example.yuejian21.nongfusanquan.yuejian21.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.yuejian21.nongfusanquan.yuejian21.R;
import com.example.yuejian21.nongfusanquan.yuejian21.adapter.SearchcourseAdapter;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Coursebean;
import com.example.yuejian21.nongfusanquan.yuejian21.javabean.Searchbean;
import com.example.yuejian21.nongfusanquan.yuejian21.model.Searchmodel;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by Nongfusanquan on 2017-10-26.
 */

public class SearchcourseActivity extends AppCompatActivity {
    private Button back_searchcourse,search_course;
    private EditText key_course;
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchcourse);
        initViews();
        initEvent();
    }

    private void initViews() {
        recyclerView = (RecyclerView)findViewById(R.id.rv_search);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局
        back_searchcourse = (Button) findViewById(R.id.back_searchcourse);
        search_course = (Button) findViewById(R.id.search_course);
        key_course =(EditText) findViewById(R.id.edsearchcourse);


    }


    private void initEvent(){
        back_searchcourse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        search_course.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String searchString = key_course. getText().toString();
                courseSearch(searchString);
            }
        });
    }
    private void courseSearch(String searchString) {
        Searchmodel Model=new Searchmodel();
        Call<List<Coursebean>> call=Model.searchCourselist(searchString);
        call.enqueue(new Callback<List<Coursebean>>() {
            @Override
            public void onResponse(Call<List<Coursebean>> call, Response<List<Coursebean>> response) {
                List<Coursebean> data = response.body();  //获取数据
                recyclerView.setAdapter(new SearchcourseAdapter(SearchcourseActivity.this,data));
            }

            @Override
            public void onFailure(Call<List<Coursebean>> call, Throwable t) {

            }
        });
    }
}